package com.hyt.it.ogt.common.utils;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

/**
 * bean conver utils <p> <b>创建日期：</b> 2022-03-16 19:05 </p>
 **/
public class PjBeanUtils {

	private static final Logger logger = LoggerFactory.getLogger(PjBeanUtils.class);

    /**
     * 描述:  将List<U>转化为List<T>
     *
     * @param destClazz 返回值的T的反射
     * @param srcList   原list值
     * @return 经过拷贝的返回list
     */
    public static <T, U> List<T> convertList(Class<T> destClazz, List<U> srcList) {
        List<T> destList = new LinkedList<T>();
        if (srcList != null && !srcList.isEmpty() && destClazz != null) {
            for (U src : srcList) {
                if (src == null) {
                    continue;
                }
                try {
                    T destDto = destClazz.newInstance();
                    BeanUtils.copyProperties(src, destDto);
                    destList.add(destDto);
                } catch (Exception e) {
                	logger.error("【convertDto】操作失败！", e);
                }
            }
        }
        return destList;
    }

    /**
     * 描述:  将U转化为T
     *
     * @return 经过拷贝的返回list
     */
    public static <T, U> T convertBean(Class<T> destClazz, U src) {
        T destDto = null;
        try {
            destDto = destClazz.newInstance();
            BeanUtils.copyProperties(src, destDto);
        } catch (Exception e) {
        	logger.error("【convertDto】操作失败！", e);
        }
        return destDto;
    }

    /**
     * 替换四个字节的字符 '\xF0\x9F\x98\x84\xF0\x9F）的解决方案
     * @param content
     * @return
     */
    public static String removeFourChar(String content) {
        if (content == null) {
            return "";
        }
        String context = content.replaceAll("[^\\u0000-\\uFFFF]", "");
        return context;
    }
}
